from PyQt5.QtWidgets import *
from myutil import globalvar as gl, logs
from myutil.api.ac_jwc_userinfo import get_xjxx_dict
from myutil.api.get_yxkc_json_from_cookie import get_free_course_list_and_yxkc_list
from ui.main import Ui_MainWindow as main_window
from win.app_about_form import app_about_form
from win.buy_jhm_form import buy_jhm_form
from win.enable_select_course_form import enable_select_course_form
from win.free_select_course_form import free_select_course_frame
from win.login_form import login_form
from win.ics_form import ics_form
from win.now_select_course_form import now_select_course_form
from win.send_feedback_form import send_feedback_form


class main_win(QMainWindow, main_window):
    def __init__(self, parent=None):
        super(main_win, self).__init__(parent)
        self.setupUi(self)

        # 主页显示
        self.show()
        # 开始 嵌入 显示登录窗口
        self.loginForm = login_form()
        self.verticalLayout.addWidget(self.loginForm)

        # 设置底部状态栏显示信息
        self.statusbar.showMessage("欢迎使用安财教务处，请先进行登录")

        # 视图设置
        self.action_view_triggered_setting()

        # action 设置
        self.action_login.triggered.connect(self.action_login_event)
        self.action_ics.triggered.connect(self.action_ics_event)
        self.action_about.triggered.connect(self.action_about_event)
        self.action_send_feedback.triggered.connect(self.action_send_feedback_event)
        self.action_update.triggered.connect(self.action_about_event)
        self.action_now_select_course.triggered.connect(self.action_now_select_course_event)
        self.action_enable_select_course.triggered.connect(self.action_enable_select_course_event)
        self.action_free_select_course.triggered.connect(self.action_free_select_course_event)
        self.action_fresh_information.triggered.connect(self.action_fresh_information_event)

    def action_fresh_information_event(self):
        logs.logs_print("刷新课程信息")
        if gl.get_value("user_info") is None:
            QMessageBox.information(self, '信息', '请先登录，再刷新课程信息', QMessageBox.Yes, QMessageBox.Yes)
            return 0

        cookie = gl.get_value("user_info")["cookie"]
        xjxx_dict = get_xjxx_dict(cookie)
        gl.set_value("xjxx_dict", xjxx_dict)
        free_course_list, yxkc_list = get_free_course_list_and_yxkc_list(cookie)
        gl.set_value("free_course_list", free_course_list)
        gl.set_value("yxkc_list", yxkc_list)
        pass

    def action_free_select_course_event(self):
        logs.logs_print("自由选课")
        # 首先去数据库里面看一下有没有激活
        # 首先看有没有登录
        if gl.get_value("user_info") is None:
            QMessageBox.information(self, '信息', '请先登录，再进行自由选课，或者已经过了选课的时间', QMessageBox.Yes, QMessageBox.Yes)
            return 0
        try:
            xjxx_dict = gl.get_value("xjxx_dict")
            user_num = xjxx_dict["学号"]

            mysql = gl.get_value("mysql_diver")
            res = mysql.get_find_list("select `is_active` from jhm_user where `jhm_uaer_xuehao`='{}'".format(user_num))
            if res[0]["is_active"] != 1:
                a = 1 / 0
        except:
            QMessageBox.information(self, '信息', '请输入激活码', QMessageBox.Yes, QMessageBox.Yes)
            logs.logs_print("输入激活码")
            self.buy_jhm_formWin = buy_jhm_form()
            self.buy_jhm_formWin.show()
            return 0

        self.statusbar.showMessage("自由选课")
        self.free_select_course_frameWin = free_select_course_frame()
        self.free_select_course_frameWin.show()

    def action_about_event(self):
        logs.logs_print("关于软件")
        self.statusbar.showMessage("关于软件")
        self.app_aboutWin = app_about_form()
        self.app_aboutWin.show()

    def action_send_feedback_event(self):
        logs.logs_print("用户反馈")
        self.statusbar.showMessage("用户反馈")

        self.fe = send_feedback_form()
        self.fe.show()

    def action_enable_select_course_event(self):
        logs.logs_print("可以选的课程查询")
        self.statusbar.showMessage("可以选的课程查询")

        if gl.get_value("user_info") is None:
            QMessageBox.information(self, '信息', '请先登录，再查看可以选的课程', QMessageBox.Yes, QMessageBox.Yes)
            return 0

        self.statusbar.showMessage("可以选的课程查询")
        # 清空 verticalLayout 里面的控件
        for i in range(self.verticalLayout.count()):
            self.verticalLayout.itemAt(i).widget().deleteLater()
        # 嵌入 显示登录窗口
        self.enable_select_course_form_Form = enable_select_course_form()
        self.verticalLayout.addWidget(self.enable_select_course_form_Form)

    def action_now_select_course_event(self):
        logs.logs_print("当前已选课程")
        self.statusbar.showMessage("当前已选课程")

        if gl.get_value("user_info") is None:
            QMessageBox.information(self, '信息', '请先登录，再查看当前已选课程', QMessageBox.Yes, QMessageBox.Yes)
            return 0

        self.statusbar.showMessage("当前已选课程")
        # 清空 verticalLayout 里面的控件
        for i in range(self.verticalLayout.count()):
            self.verticalLayout.itemAt(i).widget().deleteLater()
        # 嵌入 显示登录窗口
        self.now_select_course_Form = now_select_course_form()
        self.verticalLayout.addWidget(self.now_select_course_Form)

    def action_login_event(self):
        logs.logs_print("用户登录")
        self.statusbar.showMessage("用户登录")
        self.statusbar.showMessage("用户登录")
        # 清空 verticalLayout 里面的控件
        for i in range(self.verticalLayout.count()):
            self.verticalLayout.itemAt(i).widget().deleteLater()
        # 嵌入 显示登录窗口
        self.loginForm = login_form()
        self.verticalLayout.addWidget(self.loginForm)

    def action_ics_event(self):
        logs.logs_print("课程表转日历")

        if gl.get_value("user_info") is None:
            QMessageBox.information(self, '信息', '请先登录，再使用课程表转日历', QMessageBox.Yes, QMessageBox.Yes)
            return 0

        self.statusbar.showMessage("课程表转日历")
        # 清空 verticalLayout 里面的控件
        for i in range(self.verticalLayout.count()):
            self.verticalLayout.itemAt(i).widget().deleteLater()
        # 嵌入 显示登录窗口
        self.icsForm = ics_form()
        self.verticalLayout.addWidget(self.icsForm)

    def action_view_triggered_setting(self):
        self.dock_widget_1.hide()
        self.dock_widget_2.hide()
        self.dock_widget_3.hide()
        self.action_dock_widget_1.setIconVisibleInMenu(False)
        self.action_dock_widget_2.setIconVisibleInMenu(False)
        self.action_dock_widget_3.setIconVisibleInMenu(False)

        self.view_show_list = [0, 0, 0]
        self.action_dock_widget_1.triggered.connect(self.action_dock_widget_1_event)
        self.action_dock_widget_2.triggered.connect(self.action_dock_widget_2_event)
        self.action_dock_widget_3.triggered.connect(self.action_dock_widget_3_event)

    def action_dock_widget_1_event(self):
        logs.logs_print("设置停靠窗口1的开关")
        self.statusbar.showMessage("设置停靠窗口1的开关")
        if self.view_show_list[0] == 1:
            self.view_show_list[0] = 0
            self.action_dock_widget_1.setIconVisibleInMenu(False)
            self.dock_widget_1.hide()
        else:
            self.view_show_list[0] = 1
            self.action_dock_widget_1.setIconVisibleInMenu(True)
            self.dock_widget_1.show()

    def action_dock_widget_2_event(self):
        logs.logs_print("设置停靠窗口2的开关")
        self.statusbar.showMessage("设置停靠窗口2的开关")
        if self.view_show_list[1] == 1:
            self.view_show_list[1] = 0
            self.action_dock_widget_2.setIconVisibleInMenu(False)
            self.dock_widget_2.hide()
        else:
            self.view_show_list[1] = 1
            self.action_dock_widget_2.setIconVisibleInMenu(True)
            self.dock_widget_2.show()

    def action_dock_widget_3_event(self):
        logs.logs_print("设置停靠窗口3的开关")
        self.statusbar.showMessage("设置停靠窗口3的开关")
        if self.view_show_list[2] == 1:
            self.view_show_list[2] = 0
            self.action_dock_widget_3.setIconVisibleInMenu(False)
            self.dock_widget_3.hide()
        else:
            self.view_show_list[2] = 1
            self.action_dock_widget_3.setIconVisibleInMenu(True)
            self.dock_widget_3.show()
